<?php

class AddressBook
{
	var $_customerId = -1;
	var $_addresses = array();
	
	function AddressBook($customerId = -1)
	{
		// TODO: Input checking
		$this->_customerId = $customerId;
		
		// TODO: If not a valid customerId, return here
		// return;
		
		$addresses_query = tep_db_query('select address_book_id, entry_gender as gender, entry_firstname as firstname, entry_lastname as lastname, entry_company as company, entry_street_address as street_address, entry_suburb as suburb, entry_city as city, entry_postcode as postcode, entry_state as state, entry_zone_id as zone_id, entry_country_id as country_id from '.TABLE_ADDRESS_BOOK.' where customers_id = "'.(int) $this->_customerId.'" order by firstname, lastname');
		
		while ($address = tep_db_fetch_array($addresses_query))
		{
			$contact = new Contact($this->_customerId, $address);
			$this->_addresses[] = $contact;
		}
	}
	
	function getEntryCount()
	{
		return sizeof($this->_addresses);
	}
	
	function getEntries()
	{
		return $this->_addresses;
	}
	
	function setPrimaryAddress($contact)
	{
		global $customer_first_name, $customer_country_id, $customer_zone_id, $customer_default_address_id;
		
		if (!is_a($contact, 'Contact'))
		{
			return;
		}
		
		$customer_first_name = $contact->getFirstName();
		$customer_country_id = $contact->getCountry();
		$customer_zone_id = (($contact->getZone() > 0) ? (int) $contact->getZone() : '0');
		$customer_default_address_id = (int) $contact->getId();
		
		$sql_data_array = array(
			'customers_firstname' => $contact->getFirstName(),
			'customers_lastname' => $contact->getLastName(),
			'customers_default_address_id' => (int) $contact->getId()
		);
		
		if (ACCOUNT_GENDER == 'true')
		{
			$sql_data_array['customers_gender'] = $contact->getGender();
		}
		
		tep_db_perform(TABLE_CUSTOMERS, $sql_data_array, 'update', 'customers_id = "'.(int) $this->_customerId.'"');
	}
	
	function getPrimaryAddress()
	{
		// TODO: We need this to be independant of the external var
		global $customer_default_address_id;
		$contact = new Contact($this->_customerId, $customer_default_address_id);
		return $contact;
	}
	
	function getAddressById($addressId)
	{
		foreach ($this->_addresses as $address)
		{
			if ($address->getId() == $addressId)
			{
				return $address;
			}
		}
		
		return false;
	}
	
	function deleteEntry($contactId)
	{
		if (is_numeric($contactId))
		{
			tep_db_query('delete from '.TABLE_ADDRESS_BOOK.' where address_book_id = "'.(int) $contactId.'" and customers_id = "'.(int) $this->_customerId.'"');
		}
	}
}

class Contact
{
	var $GENDER_MALE = 'm';
	var $GENDER_FEMALE = 'f';
	
	var $_customerId;
	var $_contactId;
	var $_formatId = 1;
	var $_gender;
	var $_company;
	var $_firstName;
	var $_lastName;
	var $_street;
	var $_suburb;
	var $_postcode;
	var $_city;
	var $_state;
	var $_country;
	var $_zone = 0;
	
	function Contact($customerId = -1, $init = '')
	{
		// TODO: Input checking
		$this->_customerId = $customerId;
		
		// TODO: If not a valid customerId, return here
		// return;
		
		// Need to set the address format type
		//$address['format_id'] = tep_get_address_format_id($address['country_id']);
		
		if ($init == '')
		{
			return;
		}
		
		elseif (is_numeric($init) && $init > 0)
		{
			$addresses_query = tep_db_query('select address_book_id, entry_gender as gender, entry_firstname as firstname, entry_lastname as lastname, entry_company as company, entry_street_address as street_address, entry_suburb as suburb, entry_city as city, entry_postcode as postcode, entry_state as state, entry_zone_id as zone_id, entry_country_id as country_id from '.TABLE_ADDRESS_BOOK.' where address_book_id = "'.(int)$init.'" and customers_id = "'.(int) $this->_customerId.'" limit 1');
			$init = tep_db_fetch_array($addresses_query);
		}
		
		if (is_array($init))
		{
			$this->_contactId = $init['address_book_id'];
			$this->_gender = $init['gender'];
			$this->_firstName = $init['firstname'];
			$this->_lastName = $init['lastname'];
			$this->_company = $init['company'];
			$this->_street = $init['street_address'];
			$this->_suburb = $init['suburb'];
			$this->_city = $init['city'];
			$this->_postcode = $init['postcode'];
			$this->_state = $init['state'];
			$this->_zone = $init['zone_id']; // TODO: map to zone object?
			$this->_country = $init['country_id']; // TODO: map to country object?
			$this->_formatId = tep_get_address_format_id($this->_country);
		}
	}
	
	function getId()
	{
		return $this->_contactId;
	}
	
	function setGender($gender)
	{
		if ($gender == $this->GENDER_MALE)
		{
			$this->_gender = $this->GENDER_MALE;
			return true;
		}
		
		elseif ($gender == $this->GENDER_FEMALE)
		{
			$this->_gender = $this->GENDER_FEMALE;
			return true;
		}
		
		else
		{
			return false;
		}
	}
	
	function getGender()
	{
		return $this->_gender;
	}
	
	function setFirstName($firstName)
	{
		$firstName = tep_db_prepare_input($firstName);
		
		if (strlen($firstName) < ENTRY_FIRST_NAME_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_firstName = $firstName;
		return true;
	}
	
	function getFirstName()
	{
		return $this->_firstName;
	}
	
	function setLastName($lastName)
	{
		$lastName = tep_db_prepare_input($lastName);
		
		if (strlen($lastName) < ENTRY_LAST_NAME_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_lastName = $lastName;
		return true;
	}
	
	function getLastName()
	{
		return $this->_lastName;
	}
	
	function setCompany($company)
	{
		$company = tep_db_prepare_input($company);
		
		if (strlen($company) < ENTRY_COMPANY_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_company = $company;
		return true;
	}
	
	function getCompany()
	{
		return $this->_company;
	}
	
	function getDisplayName()
	{
		// TODO: Display the name based on preferences;
		
		if (strlen($this->_company) > 0)
		{
			return $this->_company.' ('.$this->_firstName.' '.$this->_lastName.')';
		}
		
		else
		{
			return $this->_firstName.' '.$this->_lastName;
		}
	}
	
	function setStreet($street)
	{
		$street = tep_db_prepare_input($street);
		
		if (strlen($street) < ENTRY_STREET_ADDRESS_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_street = $street;
		return true;
	}
	
	function getStreet()
	{
		return $this->_street;
	}
	
	function setSuburb($suburb)
	{
		$suburb = tep_db_prepare_input($suburb);
		
		if (strlen($suburb) < ENTRY_SUBURB_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_suburb = $suburb;
		return true;
	}
	
	function getSuburb()
	{
		return $this->_suburb;
	}
	
	function setPostcode($postcode)
	{
		$postcode = tep_db_prepare_input($postcode);
		
		if (strlen($postcode) < ENTRY_POSTCODE_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_postcode = $postcode;
		return true;
	}
	
	function getPostcode()
	{
		return $this->_postcode;
	}
	
	function setCity($city)
	{
		$city = tep_db_prepare_input($city);
		
		if (strlen($city) < ENTRY_CITY_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_city = $city;
		return true;
	}
	
	function getCity()
	{
		return $this->_city;
	}
	
	function setState($state)
	{
		$state = tep_db_prepare_input($state);
		$check_query = tep_db_query('select count(*) as total from '.TABLE_ZONES.' where zone_country_id = "'.(int)$this->_country.'"');
		$check = tep_db_fetch_array($check_query);
		$entry_state_has_zones = ((boolean) $check['total'] > 0);
		
		if ($entry_state_has_zones)
		{
			return setZone($state);
		}
		
		if (strlen($state) < ENTRY_STATE_MIN_LENGTH)
		{
			return false;
		}
		
		$this->_state = $state;
		return true;
	}
	
	function getState()
	{
		return $this->_state;
	}
	
	function setCountry($country)
	{
		$country = tep_db_prepare_input($country);
		
		// TODO: check if the country exists in the database
		
		if (!is_numeric($country))
		{
			return false;
		}
		
		$this->_country = $country;
		return true;
	}
	
	function getCountry()
	{
		return $this->_country;
	}
	
	function setZone($state)
	{
		$state = tep_db_prepare_input($state);
		$check_query = tep_db_query('select count(*) as total from '.TABLE_ZONES.' where zone_country_id = "'.(int)$this->_country.'"');
		$check = tep_db_fetch_array($check_query);
		$entry_state_has_zones = ((boolean) $check['total'] > 0);
		
		if (!$entry_state_has_zones)
		{
			return setState($state);
		}
		
		$zone_query = tep_db_query('select distinct zone_id from '.TABLE_ZONES.' where zone_country_id = "'.(int) $this->_country.'" and (zone_name like "'.tep_db_input($state).'%" or zone_code like "%'.tep_db_input($state).'%")');
		
		if (!tep_db_num_rows($zone_query) == 1)
		{
			return false;
		}
		
		$this->_zone = tep_db_fetch_array($zone_query);
		return true;
	}
	
	function getZone()
	{
		return $this->_zone;
	}
	
	function getAddressFormatted($html = true, $boln = '', $eoln = "\n")
	{
		$formatId = $this->_formatId;
		$address_format_query = tep_db_query("select address_format as format from " . TABLE_ADDRESS_FORMAT . " where address_format_id = '" . (int)$formatId . "'");
		$address_format = tep_db_fetch_array($address_format_query);
		
		$company = $this->_company;
		
		if (tep_not_null($this->_firstname))
		{
			$firstname = $this->_firstname;
			$lastname = $this->_lastname;
		}
		
		// TODO: Where did this come from?
		/*elseif (tep_not_null($address['name'])) {
		$firstname = tep_output_string_protected($address['name']);
		$lastname = '';
		}*/
		
		else
		{
			$firstname = '';
			$lastname = '';
		}
		
		$street = $this->_street;
		$suburb = $this->_suburb;
		$city = $this->_city;
		$state = $this->_state;
		
		if (tep_not_null($this->_country))
		{
			$country = tep_get_country_name($this->country);
			
			if (tep_not_null($this->_zone))
			{
				$state = tep_get_zone_code($this->country, $this->_zone, $state);
			}
		}
		
		elseif (tep_not_null($this->_country))
		{
			$country = tep_get_country_name($this->_country);
		}
		
		else
		{
			$country = '';
		}
		
		$postcode = $this->_postcode;
		$zip = $postcode;
		
		if ($html) {
			// HTML Mode
			$HR = '<hr />';
			$hr = '<hr />';
			
			if (($boln == '') && ($eoln == "\n"))
			{ // Values not specified, use rational defaults
				$CR = '<br />';
				$cr = '<br />';
				$eoln = $cr;
			}
			
			else
			{ // Use values supplied
				$CR = $eoln . $boln;
				$cr = $CR;
			}
		}
		
		else
		{
			// Text Mode
			$CR = $eoln;
			$cr = $CR;
			$HR = '----------------------------------------';
			$hr = '----------------------------------------';
		}
		
		$statecomma = '';
		$streets = $street;
		if ($suburb != '')
		{
			$streets = $street . $cr . $suburb;
		}
		
		if ($country == '')
		{
			$country = tep_get_country_name($this->_country);
			$country = tep_output_string_protected($country);
		}
		
		if ($state != '')
		{
			$statecomma = $state . ', ';
		}
		
		$fmt = $address_format['format'];
		eval("\$address = \"$fmt\";");
		
		if ((ACCOUNT_COMPANY == 'true') && (tep_not_null($company))) {
			$address = $company . $cr . $address;
		}
		
		return $address;
	}
	
	function exists()
	{
		return ((boolean) $this->_contactId > 0);
	}
	
	function saveChanges()
	{
		// TODO: This probably needs tidying up but should be mostly in place
		
		$sql_data_array = array(
			'entry_firstname' => $this->_firstName,
			'entry_lastname' => $this->_lastName,
			'entry_street_address' => $this->_street,
			'entry_postcode' => $this->_postcode,
			'entry_city' => $this->_city,
			'entry_country_id' => $this->_country
		);
		
		if (ACCOUNT_GENDER == 'true')
		{
			$sql_data_array['entry_gender'] = $this->_gender;
		}
		
		if (ACCOUNT_COMPANY == 'true')
		{
			$sql_data_array['entry_company'] = $this->_company;
		}
		
		if (ACCOUNT_SUBURB == 'true')
		{
			$sql_data_array['entry_suburb'] = $this->_suburb;
		}
		
		if (ACCOUNT_STATE == 'true')
		{
			if ($this->_zone > 0)
			{
				$sql_data_array['entry_zone_id'] = $this->_zone;
				$sql_data_array['entry_state'] = '';
			}
			
			else
			{
				$sql_data_array['entry_zone_id'] = '0';
				$sql_data_array['entry_state'] = $this->_state;
			}
		}
		
		if ($this->_contactId > 0)
		{
			tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array, 'update', "address_book_id = '".$this->_contactId."' and customers_id ='" . (int)$this->_customerId . "'");
		}
		
		else
		{
			$sql_data_array['customers_id'] = (int)$this->_customerId;
			tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);
			$new_address_book_id = tep_db_insert_id();
		}
	}
}

?>
